﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace graphical_function_explorer.FunctionPreparer
{
    class ErrorInformer
    {
        private Label errorInformerLabel;

        private char functionVar;

        private bool errorFound;

        public ErrorInformer(Label errorInformerLabel)
        {
            this.errorInformerLabel = errorInformerLabel;
        }

        public bool FindErrors(char mainVar, string equation)
        {
            functionVar = ItemProvider.GetFunctionVariable(mainVar);
            errorFound = false;

            FindUndesiredSign(equation);
            if (errorFound == false)
                FindBracketsError(equation);
            if (errorFound == false)
                FindWrongNrOfOperatorsError(equation);
            if (errorFound == false)
                FindOperatorOnWrongPositionError(equation);

            return errorFound;
        }

        /* Finds undesired signs
         * The following signs are accepted:
         * - Numbers --> 0,1,2,3,4,5,6,7,8,9
         * - Operators --> +,-,*,/
         * - Brackets --> (,)
         * - Variables --> function-variable
         */
        private void FindUndesiredSign(string equation)
        {
            for (int q = 0; q < equation.Length; q++)
            {
                if (equation[q] != '0' && equation[q] != '1' && equation[q] != '2' && equation[q] != '3' && equation[q] != '4'
                    && equation[q] != '5' && equation[q] != '6' && equation[q] != '7' && equation[q] != '8' && equation[q] != '9'
                        && equation[q] != '+' && equation[q] != '-' && equation[q] != '*' && equation[q] != '/'
                            && equation[q] != '(' && equation[q] != ')'
                                && equation[q] != functionVar)
                {
                    errorInformerLabel.Text = "The sign " + equation[q] + " is not allowed";
                    errorFound = true;
                    break;
                }
            }
        }

        /* Checks if the number of opening and closing brackets is equal
         */
        private void FindBracketsError(string equation)
        {
            int nrOfOpenBrackets = 0;
            int nrOfClosedBrackets = 0;

            for (int q = 0; q < equation.Length; q++)
            {
                if (equation[q] == '(')
                    nrOfOpenBrackets++;
                else if (equation[q] == ')')
                    nrOfClosedBrackets++;
            }

            if (nrOfOpenBrackets != nrOfClosedBrackets)
            {
                errorInformerLabel.Text = "The number of opened and closed brackets do not correspond";
                errorFound = true;
            }
        }

        /* Checks if there are consecutive operators
         */
        private void FindWrongNrOfOperatorsError(string equation)
        {
            bool hasPrecOperator = false;

            for (int q = 0; q < equation.Length; q++)
            {
                if (equation[q] == '+' || equation[q] == '-' || equation[q] == '*' || equation[q] == '/')
                {
                    if (hasPrecOperator == true)
                    {
                        errorInformerLabel.Text = "The equation contains too many operators";
                        errorFound = true;
                        break;
                    }
                    hasPrecOperator = true;
                }
                else
                    hasPrecOperator = false;
            }
        }

        /* Finds operators on wrong positions like:
         * - Beginning of the equation
         * - End of the equation
         * - Right after the open bracket
         * - Right before a closing bracket
         */
        private void FindOperatorOnWrongPositionError(string equation)
        {
            if (equation[0] == '*' || equation[0] == '/')
            {
                errorInformerLabel.Text = "There can not be such a sign at the beginning of the equation";
                errorFound = true;
            }
            else if (equation[equation.Length - 1] == '+' || equation[equation.Length - 1] == '-'
                || equation[equation.Length - 1] == '*' || equation[equation.Length - 1] == '/')
            {
                errorInformerLabel.Text = "The equation can not be concluded with an operator";
                errorFound = true;
            }
            else if (equation.Contains("(*") || equation.Contains("(/"))
            {
                errorInformerLabel.Text = "There can not be a multiplication or division sign after a open bracket";
                errorFound = true;
            }
            else if (equation.Contains("+)") || equation.Contains("-)") || equation.Contains("*)") || equation.Contains("/)"))
            {
                errorInformerLabel.Text = "There can not be an operator before a closing bracket";
                errorFound = true;
            }
        }
    }
}
